package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class handleMessage {
    @Autowired
    private IApArticleService apArticleService;
    @KafkaListener(topics = "${topic.upDownTopic}")

    private void handleMessage(ConsumerRecord<String,String> record){
//        从key里获得文章id

        String key = record.key();
        long articleId = Long.parseLong(key);
//        获得上下架的状态
        String value = record.value();
        Integer isDown=(value.equals("1")?0:1);
        System.out.println("接收到上下架的消息:articleId="+articleId+",isDowm="+isDown);
        LambdaUpdateWrapper<ApArticle> update= new LambdaUpdateWrapper<>();
        update.eq(ApArticle::getId,articleId);
        update.set(ApArticle::getIsDown,isDown);
        apArticleService.update(update);
    }
}
